package com.easylive.web.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.easylive.component.RedisComponent;
import com.easylive.entity.config.AppConfig;
import com.easylive.entity.constants.Constants;
import com.easylive.entity.dto.TokenUserInfoDto;
import com.easylive.entity.query.UserInfoQuery;
import com.easylive.entity.po.UserInfo;
import com.easylive.entity.vo.ResponseVO;
import com.easylive.exception.BusinessException;
import com.easylive.redis.RedisUtils;
import com.easylive.service.UserInfoService;
import com.wf.captcha.ArithmeticCaptcha;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.Email;
import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.Pattern;
import javax.validation.constraints.Size;

/**
 * 用户信息表 Controller
 */
@RestController
@Validated
@RequestMapping("/account")
public class AccountController extends ABaseController{

	@Resource
	private UserInfoService userInfoService;

	@Resource
    private RedisComponent redisComponent;
    @Autowired
    private AppConfig appConfig;

	@RequestMapping("/hi")
	public ResponseVO hi()
	{
		return getSuccessResponseVO("hi");
	}

	@RequestMapping("/checkCode")
	public ResponseVO checkCode() {
		ArithmeticCaptcha captcha = new ArithmeticCaptcha(100, 42);
		String code = captcha.text();
		String checkCodeKey = redisComponent.saveCheckCode(code);
		String checkCodeBase64 = captcha.toBase64();
		Map<String,String> result = new HashMap<>();
		result.put("checkCode",checkCodeBase64);
		result.put("checkCodeKey",checkCodeKey);
		return getSuccessResponseVO(result);



	}
	@RequestMapping("/register")
	public ResponseVO register(@NotEmpty @Email @Size(max = 150) String email,
							   @NotEmpty @Size(max =20) String nickName,
							   @NotEmpty @Pattern(regexp = Constants.REGRX_PASSWORD) String registerPassword,
							   @NotEmpty String checkCodeKey,
							   @NotEmpty String checkCode){

		try {
			if(!checkCode.equalsIgnoreCase(redisComponent.getCheckCode(checkCodeKey))){
				throw new BusinessException("图片验证码不正确");
			}
			userInfoService.register(email, nickName, registerPassword);
			return getSuccessResponseVO(null);
		}finally {
			redisComponent.cleanCheckCode(checkCodeKey);
		}

	}

	@RequestMapping("/login")
	public ResponseVO login(HttpServletResponse response,
							HttpServletRequest request,
							@NotEmpty @Email String email,
							@NotEmpty String password,
							@NotEmpty String checkCodeKey,
							@NotEmpty String checkCode) {

		try {
			if(!checkCode.equalsIgnoreCase(redisComponent.getCheckCode(checkCodeKey))){
				throw new BusinessException("图片验证码不正确");
			}
			String ip = getIpAddr();
			TokenUserInfoDto login = userInfoService.login(email, password, ip);
			saveToken2Cookie(response, login.getToken());

			return getSuccessResponseVO(login);
		}finally {
			redisComponent.cleanCheckCode(checkCodeKey);
			Cookie[] cookies = request.getCookies();
			String token = null;
			if(cookies != null){
				for(Cookie cookie: cookies){
					if(cookie.getName().equals(Constants.TOKEN_WEB)){
						token = cookie.getValue();
					}
				}
				if(!token.isEmpty()){
					redisComponent.cleanTokenInfo(token);
				}
			}
		}

	}

	@RequestMapping("/autoLogin")
	public ResponseVO autoLogin(HttpServletResponse response) {
		TokenUserInfoDto tokenUserInfoDto = getTokenUserInfoDto();
		if(tokenUserInfoDto == null){
			return getSuccessResponseVO(null);
		}
		if(tokenUserInfoDto.getExpireAt()-System.currentTimeMillis() < Constants.REDIS_KEY_EXPIRES_ONE_DAY){
			redisComponent.saveTokenInfo(tokenUserInfoDto);
			saveToken2Cookie(response, tokenUserInfoDto.getToken());
		}
		saveToken2Cookie(response, tokenUserInfoDto.getToken());
		return getSuccessResponseVO(tokenUserInfoDto);

	}

	@RequestMapping("/logout")
	public ResponseVO logout(HttpServletResponse response) {
		cleanCookie(response);
		return getSuccessResponseVO(null);
	}

	@RequestMapping("/getUserCountInfo")
	public ResponseVO getUserCountInfo() {
		UserInfo userInfo = userInfoService.getUserInfoByUserId(getTokenUserInfoDto().getUserId());
		userInfo.setAvatar(userInfo.getAvater());
		return getSuccessResponseVO(userInfo);
	}

}